<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-16
 * Time: 下午4:06
 */
 
include '../bootstrap.php';

$_user = Model::getInstance('user');
$user_info = $_user->needLogin();

$user_name = $_input->post('user_name');
$password = $_input->post('password');
$repassword = $_input->post('repassword');


if(!$user_name){
    $data = array(
        "statusCode"=>"300",
        "message"=>"必须填写用户名",
    );
    echo Response::JSON($data);
    exit;
}

if(!$password || $password != $repassword){
    $data = array(
        "statusCode"=>"300",
        "message"=>"两次输入的密码不一致",
    );
    echo Response::JSON($data);
    exit;
}

$email = $_input->post('email');
$tel = $_input->post('tel');
$real_name = $_input->post('real_name');

$ret = Model::getInstance('user')->addUser($user_name,$password,$email,$tel,$real_name);

$role_ids = $_input->post('role_ids',false);

if($ret){

    if($role_ids){
        $role_ids = explode(',',$role_ids);
        $role_ids = array_unique($role_ids);
        foreach($role_ids as $v){
            if($v){
                Model::getInstance('userhasrole')->add($ret,$v);
            }
        }
    }
    
    $data = array(
        "statusCode"=>"200",
        "message"=>"添加成功",
        "navTabId"=>"user",
    );
    echo Response::JSON($data);
}else{

    $data = array(
        "statusCode"=>"300",
        "message"=>"添加失败",
    );
    echo Response::JSON($data);
}